package com.example.hyg_veintype.activity.bleConnect.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.hyg_veintype.R;
import com.example.hyg_veintype.data.local.analyseLocalBean.PluseDataWraper;
import com.example.hyg_veintype.base.baseadapter.BaseHolder;
import com.example.hyg_veintype.base.baseadapter.BaseRecyclerAdapter;

import java.util.List;


/**
 * @author: yyw
 * time: 2020/12/14 14:59
 * desc: 脉诊检测位置列表适配器
 */
public class SelectPositionAdapter extends BaseRecyclerAdapter<PluseDataWraper> {

    OnItemClick onItemClick;

    public interface OnItemClick {
        void itemClicked(List<PluseDataWraper> infos, int position);
    }

    public SelectPositionAdapter(List<PluseDataWraper> infos, Context context, OnItemClick onItemClick) {
        super(infos, context);
        this.onItemClick = onItemClick;
    }

    public void updataList(List<PluseDataWraper> infos) {
        mInfos = infos;
        notifyDataSetChanged();
    }

    @Override
    public BaseHolder<PluseDataWraper> getHolder(View v, int viewType) {
        return new MyHolder(v, context);
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.veintype_item_veintype_position;
    }

    class MyHolder extends BaseHolder<PluseDataWraper> {

        RelativeLayout rlPosition;
        TextView tvPositionName;
        ImageView imgResult;

        public MyHolder(View itemView, Context context) {
            super(itemView, context);
            rlPosition = itemView.findViewById(R.id.ln_position);
            tvPositionName = itemView.findViewById(R.id.tv_position_name);
            imgResult = itemView.findViewById(R.id.img_result);
        }

        @Override
        public void setData(PluseDataWraper data, int position) {

            tvPositionName.setText(data.getPositionName());
            //检测完成显示对号
            if (data.getStatus() == 2) {
                imgResult.setVisibility(View.VISIBLE);
                rlPosition.setSelected(true);
                tvPositionName.setSelected(true);
            } else {
                //选中事件
                imgResult.setVisibility(View.GONE);
                if (data.isSelected()) {
                    rlPosition.setSelected(true);
                    tvPositionName.setSelected(true);
                } else {
                    rlPosition.setSelected(false);
                    tvPositionName.setSelected(false);
                }
            }

            rlPosition.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClick != null) {
                        onItemClick.itemClicked(mInfos, position);
                    }
//                    for (PositionBean mInfo : mInfos) {
//                        mInfo.setSelected(false);
//                    }
//                    data.setSelected(true);
//                    mInfos.set(position, data);
//                    notifyDataSetChanged();
                }
            });
        }
    }
}
